package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.updateMsg;

import com.alibaba.fastjson.JSON;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.enums.RespCodeEnum;
import com.cqhilink.iot.jobstandardmanagement.facade.utils.BackMessage;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.JsmUpdateInfoMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.JsmUpdateInfo;
import com.cqhilink.iot.jobstandardmanagement.facade.service.updateMsg.JsmUpdateInfoService;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 卢曼成
 * @Description
 * @CreateDateTime 2017-07-31 11:44
 **/
public class JsmUpdateInfoServiceImpl implements JsmUpdateInfoService {
    @Autowired
    private JsmUpdateInfoMapper jsmUpdateInfoMapper;

    /**
     * 物理删除
     *
     * @param upInfoId
     * @return
     */
    @Override
    public Map<String, Object> delete(String upInfoId) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isEmpty(upInfoId)) {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", BackMessage.DEL_FAIL);
        }
        int num = jsmUpdateInfoMapper.deleteByPrimaryKey(upInfoId);
        if (num > 0) {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            map.put("msg", BackMessage.DEL_OK);
        } else {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", BackMessage.DEL_FAIL);
        }
        return map;
    }

    /**
     * 物理批量删除
     *
     * @param upInfoIds
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> deleteList(String upInfoIds) throws Exception {
        Map<String, Object> map = Maps.newHashMap();
        if (StringUtils.isEmpty(upInfoIds)) {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", "参数错误!");
        }
        List<String> list = JSON.parseArray(upInfoIds, String.class);
        int num = jsmUpdateInfoMapper.deleteListKey(list);
        if (num > 0) {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            map.put("msg", BackMessage.DEL_OK);
        } else {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", BackMessage.DEL_FAIL);
        }
        return map;
    }

    /**
     * 添加
     *
     * @param record
     * @return
     */
    @Override
    public Map<String, Object> save(JsmUpdateInfo record) throws Exception {
        record.setStatus(0);
        record.setUpInfoId(UniqueUtil.uuid());
        int num = jsmUpdateInfoMapper.insert(record);
        Map<String, Object> map = new HashMap<String, Object>();
        if (num > 0l) {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            map.put("msg", BackMessage.ADD_OK);
        } else {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", BackMessage.ADD_FAIL);
        }
        return map;
    }

    /**
     * 根据ID查询
     *
     * @param upInfoId
     * @return
     */
    @Override
    public JsmUpdateInfo selectByPrimaryKey(String upInfoId) throws Exception {
        if (StringUtils.isEmpty(upInfoId)) {
            return null;
        }
        return jsmUpdateInfoMapper.selectByPrimaryKey(upInfoId);
    }

    /**
     * 修改
     *
     * @param record
     * @return
     */
    @Override
    public Map<String, Object> update(JsmUpdateInfo record) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isEmpty(record.getUpInfoId())) {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", BackMessage.MODIFY_FAIL);
        }
        int num = jsmUpdateInfoMapper.updateByPrimaryKeySelective(record);
        if (num > 0) {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            map.put("msg", BackMessage.MODIFY_OK);
        } else {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", BackMessage.MODIFY_FAIL);
        }
        return map;
    }

    /**
     * 分页查询
     *
     * @param record
     * @return
     */
    @Override
    public Map<String, Object> queryList(JsmUpdateInfo record) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isEmpty(record.getUpMsgId())) {
            map.put("total", 0);
            map.put("rows", null);
            return map;
        }
        List<JsmUpdateInfo> list = jsmUpdateInfoMapper.queryList(record);
        int count = jsmUpdateInfoMapper.queryCount(record);
        map.put("total", count);
        map.put("rows", list);
        return map;
    }
}
